package com.xinchen.aiowaste.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.blankj.utilcode.util.AppUtils
import com.kongqw.network.monitor.enums.NetworkState
import com.maple.utils.ext.toVisible
import com.maple.utils.util.LogUtils
import com.maple.utils.widget.update.CustomUpdateParser
import com.xinchen.aiowaste.R
import com.xinchen.aiowaste.app.Config
import com.xinchen.aiowaste.base.BaseFragment
import com.xuexiang.xupdate.XUpdate

class VersionFragment : BaseFragment() {

    private var tvVersionName: TextView? = null
    private var btnUpdate: Button? = null
    override fun hasNavController(): Boolean  = true

    override fun hasNetworkState(): Boolean = true

    override fun getLayoutId(): Int = R.layout.fragment_version

    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        setTitle("版本更新")
        showClockTime()
        onBackClick {
            navController?.popBackStack()
        }
        tvVersionName = view.findViewById(R.id.tv_version_name)
        btnUpdate = view.findViewById(R.id.btn_update)
        tvVersionName?.text = "当前版本：V${AppUtils.getAppVersionName()}"
        btnUpdate?.setOnClickListener {
            checkVersion()
        }
    }
    override fun initData(savedInstanceState: Bundle?) {

    }

    private fun checkVersion() {
        val updateUrl = "http://114.116.13.190:8180/common/app-update/latestVersion?type=${Config.getAppName()}"
        XUpdate.newBuild(requireContext())
            .updateUrl(updateUrl)
            .isGet(true)
            .updateParser(CustomUpdateParser())
            .update()
    }
}